package com.sys.user.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/4/18.
 */
public class BaseController<T> {

    protected final Logger log = LoggerFactory.getLogger(BaseController.class);

    protected void writeJson(HttpServletResponse response, String result) {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.write(result);
        } catch (Exception e) {
            log.error("error:", e);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
    protected void jsonResult(int status,String code, String msg,Object data,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", status);
        map.put("code", code);
        map.put("msg", msg);
        map.put("result", data);
        writeJson(response, JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));
    }
   /* @InitBinder
    protected void initBinder(WebDataBinder binder) {
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
//        binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));
        binder.registerCustomEditor(int.class, new IntegerEditor());
//        binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
        binder.registerCustomEditor(long.class, new LongEditor());
        binder.registerCustomEditor(double.class, new DoubleEditor());
        binder.registerCustomEditor(float.class, new FloatEditor());
    }*/
    /**
     * 得到是图片的集合 或者得到不是图片的集合
     * @param multipartFiles
     * @param type 0是视频 1是图片
     * @return
     * @throws IOException
     */
    public List<MultipartFile> getImageYN(MultipartFile[] multipartFiles, int type) throws IOException {
        List<MultipartFile> list = new ArrayList<MultipartFile>();
        if(multipartFiles==null||multipartFiles.length==0){
            return new ArrayList<MultipartFile>();
        }
        for (MultipartFile multipartFile : multipartFiles) {
            if(multipartFile.getSize()>0) {
                String filetype = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
//                String mimetype = FileManage.getMimeType(multipartFile.getOriginalFilename());
                boolean b = false;
                if (StringUtils.isNotBlank(filetype)&&type == 1) {
                    String filetypev =  filetype.toUpperCase();
                    if(filetypev.equals("JPG")||filetypev.equals("JPEG")||filetypev.equals("GIF")||filetypev.equals("PNG")||filetypev.equals("BMP")){
                        b = true;
                    }
                } else if (StringUtils.isNotBlank(filetype)&&type == 0) {
                    String filetypev =  filetype.toUpperCase();
                    if(filetypev.equals("AVI")||filetypev.equals("RM")||filetypev.equals("RMVB")||filetypev.equals("WMV")||filetypev.equals("MP4")
                            ||filetypev.equals("MPG")||filetypev.equals("MPEG")||filetypev.equals("MPE")||filetypev.equals("MOV")
                            ||filetypev.equals("FLV") ||filetypev.equals("MKV")||filetypev.equals("3GP")){
                        b = true;
                    }
                }
                if (b) {

                    String name = multipartFile.getName();
                    String originalFilename = multipartFile.getOriginalFilename();
                    String contentType = multipartFile.getContentType();
                    InputStream contentStream = multipartFile.getInputStream();
                    MultipartFile file = new MockMultipartFile(name, originalFilename, contentType, contentStream);
                    list.add(file);
                }
            }

        }
        return list;
    }



}
